//
//  LBModalViewController.m
//  LBBase
//
//  Created by Gary on 15/7/1.
//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "LBModalViewController.h"
#import "BaseViewController.h"
#import "BaseTableViewController.h"
#import "Common.h"

@interface LBModalViewController ()

@property (nonatomic, retain) UIViewController *tempViewController;

@end

@implementation LBModalViewController

- (id)initWithModalVC:(UIViewController*)modalVC animated:(BOOL)animated {
    self.tempViewController = [[UIViewController alloc] init];
    self = [super initWithRootViewController:self.tempViewController];
    self.modalVC = modalVC;
    [self pushViewController:self.modalVC animated:animated];
    return self;
}

- (void)dismissModalViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
    NSArray *arr = self.viewControllers;
    for (NSInteger i = arr.count - 1; i >= 0; i--) {
        UIViewController *vc = arr[i];
        if ([vc isKindOfClass:[BaseViewController class]]) {
            [(BaseViewController*)vc unregisterBusinessListener];
        } else if ([vc isKindOfClass:[BaseTableViewController class]]) {
            [(BaseTableViewController*)vc unregisterBusinessListener];
        }
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:flag completion:completion];
    });
}

- (BOOL)shouldAutorotate {
    return [self.viewControllers.lastObject shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}

@end
